package com.example.cofeed.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.activity.PushSubscriptionActivity;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.Attention;
import com.example.cofeed.entity.Catalogs;
import com.example.cofeed.entity.City;
import com.lzy.okgo.model.Response;

/**
 * Created by mqwang on 2017/11/15.
 */

public class AttentionAdapter extends BaseQuickAdapter<Attention, BaseViewHolder> {

    public AttentionAdapter() {
        super(R.layout.item_push_subscription_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, final Attention item) {
        if (item.isSelect) {
            helper.getView(R.id.rec_lv).setVisibility(View.VISIBLE);
            helper.setImageResource(R.id.select_iv, R.mipmap.ic_up);
        } else {
            helper.getView(R.id.rec_lv).setVisibility(View.GONE);
            helper.setImageResource(R.id.select_iv, R.mipmap.ic_down);
        }

        helper.setText(R.id.name_tv, item.catname);

        AttentionSubAdapter cityAdapter = new AttentionSubAdapter();
        RecyclerView recLv = helper.getView(R.id.rec_lv);
        recLv.setLayoutManager(new LinearLayoutManager(mContext));
        recLv.setAdapter(cityAdapter);
        cityAdapter.setNewData(item.chinfo);

        cityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final Attention city = (Attention) adapter.getData().get(position);
                ((AttentionSubAdapter) adapter).setSelect(city);
                if (city.ispush == 1) {
                    ApiService.followinfo_on_off("pushSubscriptionSet",1, item.catid, city.chid, new DialogCallback<LzyResponse<Object>>() {
                        @Override
                        public void onSuccess(Response<LzyResponse<Object>> response) {
//                                city.ispush = 1;
//                                notifyDataSetChanged();
                        }
                    });
                } else {
                    ApiService.followinfo_on_off("pushSubscriptionSet",0, item.catid, city.chid, new DialogCallback<LzyResponse<Object>>() {
                        @Override
                        public void onSuccess(Response<LzyResponse<Object>> response) {
//                                city.ispush = 1;
//                                notifyDataSetChanged();
                        }
                    });
                }

            }
        });
    }


    public void setSelect(Attention select) {
        select.isSelect = !select.isSelect;
        notifyDataSetChanged();
    }

}
